\documentclass[a4paper]{article}
\usepackage{xltxtra}
\usepackage{graphicx}
\usepackage{fontspec}
\usepackage{cite}
\usepackage{geometry}
\usepackage{amsmath}

\geometry{left=20mm,right=20mm,top=20mm,bottom=20mm}
\begin{document}  

\title{HW2: Programming}  
\author{Jiang Zhou 3220101339}   
\date{2024-10-27}  
\maketitle  

\section{A:}  
The header file \texttt{Interpolation.hpp} defines a base class \texttt{Interpolation} and three derived classes: \texttt{NewtonInterpolation}, \texttt{HermiteInterpolation}, and \texttt{BezierInterpolation}. These classes implement different interpolation algorithms.

\subsection{Base Class: Interpolation}

\begin{itemize}
    \item Pure virtual function \texttt{interpolate(double x)}: Performs interpolation at a specific point $x$, returning a vector of interpolation results.
    \item Virtual destructor \texttt{~Interpolation()}: Ensures the correct invocation of the destructor for derived classes.
\end{itemize}

\subsection{Derived Class: NewtonInterpolation}

\begin{itemize}
    \item Inherits from \texttt{Interpolation}.
    \item Member variables:
        \begin{itemize}
            \item \texttt{xVec}: Stores the x-coordinates of the interpolation points.
            \item \texttt{yVec}: Stores the y-coordinates of the interpolation points.
        \end{itemize}
    \item Member functions:
        \begin{itemize}
            \item Constructor: Accepts \texttt{xVec} and \texttt{yVec} as parameters and checks if their sizes are equal.
            \item \texttt{interpolate(double x)}: Implements the Newton interpolation algorithm, returning the interpolation result at $x$.
        \end{itemize}
\end{itemize}

\subsection{Derived Class: HermiteInterpolation}

\begin{itemize}
    \item Inherits from \texttt{Interpolation}.
    \item Member variables:
        \begin{itemize}
            \item \texttt{M}: Stores the interpolation data matrix, where the first row is the x-coordinates, and the remaining rows are the corresponding y-coordinates and derivatives.
            \item \texttt{x}: Stores the x-coordinates of the interpolation points.
            \item \texttt{c}: Number of columns, representing the dimension of the data.
            \item \texttt{r}: Number of rows minus one, representing the number of interpolation points.
        \end{itemize}
    \item Member functions:
        \begin{itemize}
            \item Constructor: Initializes the data matrix \texttt{M}, computes the x-coordinates \texttt{x}, and the number of columns \texttt{c} and rows \texttt{r}.
            \item \texttt{interpolate(double x1)}: Implements the Hermite interpolation algorithm, returning the interpolation result at $x1$.
        \end{itemize}
\end{itemize}

\subsection{Derived Class: BezierInterpolation}

\begin{itemize}
    \item Inherits from \texttt{Interpolation}.
    \item Member variables:
        \begin{itemize}
            \item \texttt{controlPoints}: Stores the control points of the Bezier curve, where each control point is a vector containing multiple dimensions.
        \end{itemize}
    \item Member functions:
        \begin{itemize}
            \item Constructor: Accepts control points as parameters and checks if they are not empty.
            \item \texttt{bernsteinPolynomial(int n, int i, double t)}: Computes the Bernstein basis polynomial.
            \item \texttt{interpolate(double t)}: Implements the Bezier curve interpolation, returning the curve point at parameter $t$.
        \end{itemize}
\end{itemize}

The test's results are as follows:
\begin{verbatim}
    NewtonInterpolation value:
    At x = 1.5: 1.5
    At x = 2.5: 2.5
    At x = 3.5: 3.5
    At x = 4.5: 4.5
    Interpolated value at x = 4: 16
    Bezier curve point at t = 0.5 is (2.5, 2.5)
\end{verbatim}

\section{B:}  
The experimental results are as follows:

\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\textwidth]{ProblemB/runge_phenomenon.png}
    \caption{Runge phenomenon}
    \label{fig:B}
\end{figure}
The Runge phenomenon refers to the behavior observed when using equidistant nodes for polynomial interpolation. As the degree of the polynomial increases, the interpolating polynomial may oscillate and diverge near the interval endpoints, even though the interpolation is exact at the nodes. This phenomenon is an important concept in numerical analysis, demonstrating that higher-degree polynomial interpolation may not always perform better, especially near the interval endpoints. By plotting interpolating polynomials of different degrees against the original function, this phenomenon can be observed visually.


\section{C:} 
The experimental results are as follows:

\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\textwidth]{ProblemC/Chebyshev_Interpolation.png}
    \caption{Chebyshev Interpolation}
    \label{fig:C}
\end{figure}

Obviously, Chebyshev interpolation avoids the large oscillations that occurred in the previous job.
\section{D:} 
The experimental results are as follows:
\begin{verbatim}
    Interpolated value at t = 10(Position): 742.503
    Interpolated value at t = 10(Speed): 48.3817
    D:
    A root of the speed = 81 is: 11.3723
\end{verbatim}
\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\textwidth]{ProblemD/speed.png}
    \caption{speed}
    \label{fig:D}
\end{figure}
\subsection{a}
The position of the car in t = 10 seconds is 742.503 and the speed is 66.0962

\subsection{b}
By taking the derivative of the Hermite polynomial, it is determined that the car at t is 11.3723 once exceeded 81 feet per second.

\section{E:} 
\subsection{a}
The experimental results are as follows:
\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\textwidth]{ProblemE/Sp.png}
    \caption{Sp}
    \label{fig:E}
\end{figure}
\subsection{b}
Both larvae samples are predicted not to die after 15 days.


\section{F:} 
The experimental results are as follows:
\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\textwidth]{ProblemF/heart.png}
    \caption{heart}
    \label{fig:F}
\end{figure}

\end{document}  
